<style>
    .form-datetime .form-date_1 .form-datetime_1{
        cursor:pointer;
    }
    .tabliv {
        display: none;
    }
    .tabliv > table th, .tabliv > table td {
        padding: 8px;
        border-bottom: 1px solid #555;
    }
</style>
<div class="panel" id="nav">
<div class="panel-heading"><h3 class="panel-title">备货需求明细管理</h3></div>
<div class="panel-body">
    <p>
        <span style="color: #91bef0"><b>Tip:</b></span><br>
        1.需求申请单据的使命完成的最终状态为已完成和取消，查询平台计划单状态需到平台计划单模块跟进;<br>
        2.数量修改只能从大往小修改，不能<span style="color: red">从小往大修改</span>，往大修改请取消重新申请;<br>
        3.合规审核提交，相同站点、相同公司SKU的合规选项会<span style="color: red">同步</span>，提交的合规选项为空会<span style="color: red">重置</span>合规选项。
        <br>
    </p>
    <form action="Inbound/Prepareneeds/all_detail" id="search-form" class="search-form">
        <div class="container-fluid">
            <div class="col-sm-12 col-md-3  add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">SKU</span>
                    <input class="form-control " placeholder="请输入内容" name="sku"
                           data-name-group="common" type="text" value="">
                </div>
            </div>
            <div class="col-sm-12 col-md-4  add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">账号</span>
                    <select class="chosen-select form-control" tabindex="-1" name="account_id" data-name-group="common">
                        <option value="" class="empty-opt">-- 请选择 --</option>
                        <volist name="accounts" id="item">
                            <option value="{$item.id}" >{$item.name}</option>
                        </volist>
                    </select>
                    <input type="hidden" id="user" value="{$Think.session.current_account.role_id}">
                </div>
            </div>
            <div class="col-sm-12 col-md-2  add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">站点</span>
                    <select class="chosen-select form-control" tabindex="-1" name="site_id" data-name-group="common">
                        <option value="" class="empty-opt">-- 请选择 --</option>
                        <volist name="sites" id="item">
                            <option value="{$key}" >{$item}</option>
                        </volist>
                    </select>
                </div>
            </div>
            <div class="col-sm-12 col-md-3  add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">状态</span>
                    <select class="chosen-select form-control" tabindex="-1" name="status" data-name-group="common">
                        <option value="" class="empty-opt">-- 请选择 --</option>
                        <volist name="orderState" id="item">
                            <option value="{$key}" >{$item}</option>
                        </volist>
                    </select>
                </div>
            </div>
            <div class="col-sm-12 col-md-3  add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">是否退税</span>
                    <select class="chosen-select form-control" tabindex="-1" name="export_tax_rebate"
                            data-name-group="common">
                        <option value="" class="empty-opt">-- 请选择 --</option>
                        <option value="1">是</option>
                        <option value="0">否</option>

                    </select>
                </div>
            </div>
            <div class="col-sm-12 col-md-4 add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">需求申请人</span>
                    <input class="form-control " placeholder="请输入正确的名字" name="create_user_id"
                           data-name-group="common" type="text" value="">
                </div>
            </div>
            <div class="col-sm-12 col-md-5  add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">单据生成时间</span>
                    <input class="form-control form-datetime" type="text" name="create_time_from" style="cursor:pointer;" readonly>
                    <span class="input-group-addon"><span class="icon-calendar"></span></span>
                    <input class="form-control form-datetime" type="text" name="create_time_to" style="cursor:pointer;" readonly>
                </div>
            </div>
            <div class="col-sm-12 col-md-5  add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">要求到货时间</span>
                    <input class="form-control form-date_1" type="text" name="claim_arrive_time_from" style="cursor:pointer;" readonly>
                    <span class="input-group-addon"><span class="icon-calendar"></span></span>
                    <input class="form-control form-date_1" type="text" name="claim_arrive_time_to" style="cursor:pointer;" readonly>
                    <span class="input-group-addon fix-border fix-padding">&nbsp;<span class="text-danger">0</span>天</span>
                </div>
            </div>
            <div class="col-sm-12 col-md-5  add-space">
                <div class="input-group input-group-md">
                    <span class="input-group-addon">合规审核时间</span>
                    <input class="form-control form-date_1" type="text" name="compliance_check_time_from" style="cursor:pointer;" readonly>
                    <span class="input-group-addon"><span class="icon-calendar"></span></span>
                    <input class="form-control form-date_1" type="text" name="compliance_check_time_to" style="cursor:pointer;" readonly>
                </div>
            </div>
            <div class="col-md-7 add-space">
                <input class="btn btn-primary jsSearchBtn" type="submit" value="搜索">
                <button class="btn btn-primary" onclick="download_csv()" type="button">导出</button>
                <button class="btn btn-primary" onclick="download_csv_other()" type="button">下载FBA备货需求计划单</button>
                <button class="btn btn-primary" onclick="download_compliance()" type="button">下载合规信息报表</button>
            </div>
        </div>
    </form>
    <div class="clearfix"></div>
    <hr>

    <div class="fixed-table-toolbar b10">

        <div id="toolbar" class="btn-group">

            <!--<button type="button" class="btn" id="change_code">批次切换</button>-->
            <!--<button type="button" class="btn" data-toggle="modal" data-target=".btn_ck">标记账号分仓</button>-->
            <button type="button" class="btn" data-toggle="modal" data-target=".btn_check">销售核对</button>
            <button type="button" class="btn" data-toggle="modal" data-target=".btn_compliance">合规审核</button>
            <button type="button" class="btn" data-toggle="modal" data-target=".preview">物流预审</button>
            <!--<button type="button" class="btn" data-toggle="modal" data-target=".wl_check">物流核对</button>-->
            <button type="button" class="btn create_fba" data-toggle="modal" data-target="#create_fba">生成FBA平台备货计划</button>
        </div>
        <div class="btn-group" style="float: right">
            <button type="button" class="btn btn-warning batch_cancel" data-toggle="modal" data-target=".btn_batch_del">批量废除行</button>
        </div>
    </div>
    <?php if(!empty($result)):?>

    <table class="table table-striped  table-hover" cellspacing="0" width="100%">
        <thead>
        <tr>
            <th style="width: 10px;"><input id="checkAll" type="checkbox"/></th>
            <th width="2%">状态</th>
            <th width="6%">SKU</th>
            <th width="6%">合规选项</th>
            <th width="6%">SellerSKU</th>
            <th width="6%">账号</th>
            <th width="2%">站点</th>
            <th width="15%">产品名称</th>
            <th width="6%">需求<br/>数量</th>
            <th width="6%">是否<br/>退税</th>
            <th width="6%">公司主体</th>
            <th width="6%">物流属性</th>
            <th width="6%">销售员</th>
            <th width="6%">采购员</th>
            <th width="10%">要求<br/>到货时间</th>
            <th width="6%">操作日志</th>
            <th width="6%">备注</th>
            <!--<th width="6%">单独发货</th>-->
            <th width="6%">货物等级</th>
            <th width="6%">操作</th>
        </tr>
        </thead>
        <tbody>

        <foreach name='result' item='vo'>
        <tr class="result_row" <if condition="($vo.complianceSpecial eq 1) AND ($vo.statusId eq 15)">style="cursor: pointer; color: red;"<else />style="cursor: pointer;"</if>>
            <td class="detail_id"><input name="subBox" type="checkbox" value="{$vo.id}"/></td>
            <td class="status"><p style=" width: 40px;word-wrap: break-word;word-break : break-all; ">{$vo.status}</p></td>
            <td class="sku"><a target="_blank" href="http://192.168.5.5:802/showsku/index?sku={$vo.sku}">{$vo.sku}</a></td>
            <td class="compliance">{$vo.compliance}</td>
            <td class="sellerSku">{$vo.seller_sku}</td>
            <td class="account_name">{$vo.account_name}</td>
            <td class="site_id">{$vo.site_id}</td>
            <td><p style=" width:200px;word-wrap: break-word;word-break : break-all;">{$vo.sku_name}</p></td>
            <td class="occupy">{$vo.needs_quantity}</td>
            <td class="export_tax_rebate">{$vo.export_tax_rebate}</td>
            <td class="enterprise_dominant">
                <p style="width:50px; white-space:nowrap;overflow:hidden;text-overflow:ellipsis; " title="{$vo.enterprise_dominant}">
                    {$vo.enterprise_dominant}
                </p>
            </td>
            <td>{$vo.logic_attr}</td>
            <td class="seller">{$vo.seller_id}</td>
            <td>{$vo.buyer_id}</td>
            <td>{$vo.claim_arrive_time}</td>
            <td id="tr{$vo.id}" class="plantr"><font color="#008b8b" style="cursor:pointer; ">展开</font></td>
            <td>{$vo.remark}</td>
            <td>{$vo.cargo_grade}</td>
<!--            <td class="singleShip">
                <select type="text" class="form-control">
                    <option value="0">否</option>
                    <option value="1" <if condition="$vo.single_ship eq 1">selected="selected"</if>>是</option>
                </select>
            </td>-->
            <td>
                <button type="button" class="btn btn-xs btn-info b10 edit_detail"  data-toggle="modal" data-target=".btn_edit" data-id="{$vo.id}">编辑
                </button>
                <button type="button" class="btn btn-xs btn-info cancel" data-toggle="modal" data-target=".btn_del" data-id="{$vo.id}" data-status="{$vo.status}">废除行
                </button>
            </td>
        </tr>
            <!-- <tr class="step">
            </tr> -->
            <tr>
                <td id="tr{$vo.id}s"  colspan="17" class="tabliv">
                    <table>
                        <thead>
                        <tr>
                            <th>创建人</th>
                            <th>创建时间</th>
                            <th>销售确认人</th>
                            <th>销售确认时间</th>
                            <th>合规组审核人</th>
                            <th>合规组审核时间</th>
                            <th>物流确认人</th>
                            <th>物流确认时间</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td>{$vo.create_user_id}</td>
                            <td>{$vo.create_time}</td>
                            <td>{$vo.seller_check_user_id}</td>
                            <td>{$vo.seller_check_time}</td>
                            <td>{$vo.compliance_check_user_id}</td>
                            <td>{$vo.compliance_check_time}</td>
                            <td>{$vo.logistic_check_user_id}</td>
                            <td>{$vo.logistic_check_time}</td>
                        </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </foreach>
        </tbody>
    </table>
    <?php endif?>
    <div class="clearfix clear"></div>
    <div class="row">
        <div class="col-md-6 pull-left">
            <label>每页 50 条记录
                显示 1 到 50 项，共 {$count}项</label>
        </div>
        <div class="col-md-6">
            <div class="page pull-right" style="padding: 0;margin: 0">
                {$page}
            </div>
        </div>
    </div>
</div>
<!-- 销售员核对 -->
<div class="modal fade btn_check">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">销售员核对</h4>
            </div>
            <div class="modal-body"><p class="text-primary">销售员确定已核对</p></div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="check(1)">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>
<!-- 合规审核对话框 -->
<div class="modal fade btn_compliance ">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">合规组人员核对</h4>
            </div>
            <div class="modal-body"><p class="text-primary">合规组人员确定已核对</p></div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="compliance()">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>
<!-- 物流预审对话框 -->
<div class="modal fade preview">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">物流人员核对</h4>
            </div>
            <div class="modal-body"><p class="text-primary">物流人员确定已核对</p></div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="preview()">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>
<!-- 创建FBA平台计划对话框 -->
<div class="modal fade" id="create_fba">
    <div class="modal-dialog modal-sm">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">创建FBA平台计划</h4>
            </div>
            <div class="modal-body">
                <p class="text-primary">确定选择创建FBA平台计划单吗?</p>
                <span style="color: red">备注(必填)：
                </span><input class="form-control" name="plan_remark" type="text" placeholder="请输入运输方式！">
            </div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="create_fba()">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>
<!-- 仓库核对话框 -->
<div class="modal fade ck_check">
    <div class="modal-dialog modal-sm">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">仓库核对</h4>
            </div>
            <div class="modal-body"><p class="text-primary">你确定仓库核对？</p></div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="check(2)">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>


<!-- 物流核对话框 -->
<div class="modal fade wl_check">
    <div class="modal-dialog modal-sm">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">物流核对</h4>
            </div>
            <div class="modal-body">
                <label>请选择承运商服务</label>
                <select class="form-control  c" tabindex="-1" name="transport_way_hedui" data-name-group="common">
                    <option value="" class="empty-opt">-- 请选择 --</option>
                    <volist name="transport_way" id="item">
                        <option value="{$key}" >{$item}</option>
                    </volist>
                </select>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="check(3)">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>


<!--编辑---->

<div class="modal fade btn_edit">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal" action="" method="post" role="form">

                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-6">
                                <!--<div class="form-group">
                                    <label class="col-md-5 control-label">备货需求批次号：</label>

                                    <div class="col-md-7">
                                        <p class="form-control-static batch_code_edit"></p>
                                    </div>
                                </div>-->
                                <div class="form-group">
                                    <label class="col-md-5 control-label">状态：</label>

                                    <div class="col-md-7">
                                        <p class="form-control-static status_edit"></p>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-5 control-label">SKU：</label>

                                    <div class="col-md-7">
                                        <p class="form-control-static sku_edit"></p>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-5 control-label">SKU中文名：</label>

                                    <div class="col-md-7">
                                        <input type="text" name="sku_cnname" class="form-control" placeholder="请输入文本" value="">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-5 control-label">账号：</label>

                                    <div class="col-md-7">
                                        <p class="form-control-static account_name_edit"></p>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-5 control-label">站点：</label>

                                    <div class="col-md-7">
                                        <p class="form-control-static site_id_edit"></p>
                                        <p class="form-control-static site" style="display:none"></p>
                                    </div>
                                </div>


                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="col-md-3 control-label">需求数量：</label>

                                    <div class="col-md-7">
                                        <input type="text" name="needs_quantity_edit" class="form-control" placeholder="请输入文本" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"></div>
                                        <input type="hidden" name="compare_quantity">
                                </div>
                                <!--<div class="form-group">
                                    <label class="col-md-3 control-label">储位：</label>

                                    <div class="col-md-7">
                                        <input type="text" name="storage_edit" class="form-control" placeholder="请输入文本"></div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-3 control-label">预拣数量：</label>

                                    <div class="col-md-7">
                                        <input type="text" name="actual_quanlity_edit" class="form-control" placeholder="请输入文本" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"></div>
                                </div>-->
                                <div class="form-group">
                                    <label class="col-md-3 control-label">是否退税：</label>

                                    <div class="col-md-7">
                                        <select class="form-control" name="export_tax_rebate_edit">
                                            <option value="">-- 请选择 --</option>
                                            <option value="0">否</option>
                                            <option value="1">是</option>
                                        </select>
                                    </div>
                                </div>
                                <!--<div class="form-group">
                                    <label class="col-md-3 control-label">承运商服务：</label>

                                    <div class="col-md-7">
                                        <select class="form-control" name="carrier_service_id_edit">
                                            <option value="" class="empty-opt">&#45;&#45; 请选择 &#45;&#45;</option>
                                            <volist name="transport_way" id="item">
                                                <option value="{$key}" >{$item}</option>
                                            </volist>
                                        </select>
                                    </div>
                                </div>-->
                                <div class="form-group">
                                    <label class="col-md-3 control-label">备注：</label>

                                    <div class="col-md-7">
                                        <input type="text" name="remark" class="form-control" placeholder="请输入文本"></div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-3 control-label">货物等级：</label>

                                    <div class="col-md-7">
                                        <select class="form-control" name="cargo_grade">
                                            <option value="">-- 请选择 --</option>
                                            <option value="A">A</option>
                                            <option value="B">B</option>
                                            <option value="C">C</option>
                                            <option value="D">D</option>
                                            <option value="E">E</option>
                                            <option value="F">F</option>
                                            <option value="G">G</option>
                                        </select>
                                        <!--<input type="text" name="cargo_grade" class="form-control" placeholder="请输入文本">-->
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-3 control-label">海外监管条件：</label>

                                    <div class="col-md-7">
                                        <textarea name="overseas_supervision" class="form-control" placeholder="请输入文本"></textarea>
                                        <!--<input type="text" name="overseas_supervision" class="form-control" placeholder="请输入文本"></div>-->
                                </div>

                            </div>

                        </div>
                    </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <input type="hidden" name="id_edit">
                <button type="button" class="btn btn-primary" onclick="edit()">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

            </div>
        </div>
    </div>
</div>


<!-- 废除行对话框 -->
<div class="modal fade btn_del">
    <div class="modal-dialog modal-sm">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">废除行</h4>
            </div>
            <div class="modal-body"><p class="text-primary">你确定要废除行？</p></div>

        </div>
        <div class="modal-footer">
            <input type="hidden" name="cancel_id">
            <input type="hidden" name="cancel_status">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="cancel()">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>
<div class="modal fade btn_batch_del">
    <div class="modal-dialog modal-sm">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">批量废除行</h4>
            </div>
            <div class="modal-body"><p class="text-primary">你确定要批量废除行？</p></div>

        </div>
        <div class="modal-footer">
            <input type="hidden" name="cancel_ids">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="batch_cancel()">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>
<!-- 当前页面js引用处 -->
<script type="text/javascript" src="__PUBLIC__/javascripts/admin/layer/layer.js"></script>
<script>
    /**
     * 展开
     */
    $(document).ready(function () {
        $(".plantr").click(function () {
            var trid = $(this).attr("id");
            $("#" + trid + "s").toggle();
        });

        $('.singleShip').each(function () {
           if ($(this).prevAll('.status').children().text() != '待物流预审') {
               $(this).children().attr('disabled', 'disabled');
           }
        });

        $('input[name="subBox"]').each(function () {
            if ($(this).parent().nextAll('.status').children().text() == "取消") {
                $(this).attr('disabled', 'disabled');
            }
        });
        var clickIndex = -1;
        $('.result_row').on("click", function () {
            if($(this).index() == clickIndex){
                $(this).siblings('.step').remove();
                clickIndex = -1;
                return
            }else{
                clickIndex = $(this).index();
                $(this).siblings('.step').remove();
                if ($(this).children('.status').children().text() == "已完成") {
                    var id = $(this).children('.detail_id').children().val();
                    var rowObj = $(this);
                        $.get("Inbound/Prepareneeds/getShipmentStatusByNeedDetailId",
                                {id : id},
                                function (status) {
                                    if (status != 0) {
                                        showProgress(status, rowObj);
                                    }
                                }
                        );
                    }
            }
        });
        // function () {
        //     $(this).next('.step').slideUp();
        // }

        $(".com_opt").click(function () {
            $(this).parent().parent().prevAll('.detail_id').children().attr('checked', true);
        });

        $("#change_code").click(function () {
            var data = [];
            var status_flag = true;
            var i = 0;
            $("input[name='subBox']:checked").each(function () {
                if($(this).parent().parent().children('td').eq(2).text() != "待销售确认"){
                    status_flag = false;
                    layer.msg("请选择状态为待销售确认的批次",{icon : 5});
                    return false;
                }else{
                    var need_change_code_info = {};
                    need_change_code_info.id = $(this).parent().parent().children('td').eq(0).children('input').val();
                    need_change_code_info.batch_code = $(this).parent().parent().children('td').eq(1).text();
                    need_change_code_info.sku = $(this).parent().parent().children('td').eq(3).text();
                    need_change_code_info.account_name = $(this).parent().parent().children('td').eq(4).text();
                    need_change_code_info.claim_arrive_time = $(this).parent().parent().children('td').eq(14).text();
                    data.push(need_change_code_info);
                }
            });

            if(status_flag == false){
                return false;
            }

            if(data.length < 1){
                layer.msg("请选择要切换的批次号",{icon : 5});
                return false;
            }

            $.ajax({
                type : 'POST',
                url  : "__URL__/changeCode",
                data : {data:data},
                dataType : "json",
                success : function (content) {
                    if(content == 1){
                        layer.msg("切换失败，别的批次号有相同备货需求！",{icon : 5});
                    }else if(content == 2){
                        layer.msg("没有可以切换的批次号",{icon : 5});
                    }else{
                        layer.open({
                            type : 1,
                            content : content,
                            area: ['700px', '400px'],
                            title : "切换批次号",
                            end : function () {
                                window.location.reload();
                            }
                        });
                    }

                },
                error : function () {
                    layer.msg("请求出错！",{icon : 5});
                }
            });
        })
    });
    /**
     * 计算日期差
     * khq 2017.1.23
     */
    $('.form-date_1').datetimepicker({
        lang:'ch',
        timepicker:false,
        format:'Y/m/d',
        formatDate:'Y/m/d',
        onChangeDateTime:function(dp,$input){
            //alert(dp);
            date_needs();
        },
        onSelectDate:function()
        {
            date_needs();
        }
    });
    var date_needs =function(){
        var start_time = Date.parse($("input[name=claim_arrive_time_from]").val());
        var end_time   = Date.parse($("input[name=claim_arrive_time_to]").val());
        var diff       = end_time-start_time;                         //时间差的毫秒数
        var days=Math.floor(diff/(24*3600*1000));
        //var leave1=diff%(24*3600*1000);                            //计算天数后剩余的毫秒数
        //var hours=Math.floor(leave1/(3600*1000))/24;               //计算出小时数
        if(!isNaN(days))
            $(".text-danger").html(days);
    };

    $(function () {
        $("input[name=create_user_id]").val("<?php echo $_GET['create_user_id']?>");
        $("input[name=sku]").val("<?php echo $_GET['sku']?>");
        $("select[name=account_id]").val("<?php echo $_GET['account_id']?>");
        $("select[name=site_id]").val("<?php echo $_GET['site_id']?>");
        $("select[name=status]").val("<?php echo $_GET['status']?>");
        $("select[name=export_tax_rebate]").val("<?php echo $_GET['export_tax_rebate']?>");
        $("input[name=create_time_from]").val("<?php echo $_GET['create_time_from']?>");
        $("input[name=create_time_to]").val("<?php echo $_GET['create_time_to']?>");
        $("input[name=claim_arrive_time_from]").val("<?php echo $_GET['claim_arrive_time_from']?>");
        $("input[name=claim_arrive_time_to]").val("<?php echo $_GET['claim_arrive_time_to']?>");
        $("input[name=compliance_check_time_from]").val("<?php echo $_GET['compliance_check_time_from']?>");
        $("input[name=compliance_check_time_to]").val("<?php echo $_GET['compliance_check_time_to']?>");

        date_needs();
        $('select.chosen-select').chosen({
            no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
            disable_search_threshold: 2, // 10 个以下的选择项则不显示检索框
            search_contains: true         // 从任意位置开始检索
        });
    });

    $(".cancel").click(function(){
        var id = $(this).attr('data-id');
        var status = $(this).attr('data-status');
        $("input[name=cancel_id]").val(id);
        $("input[name=cancel_status]").val(status);
    });

    $(".batch_cancel").click(function () {
        var status_flag = true;
        var detail_ids = [];
        $("input[name='subBox']:checked").each(function () {
            if($(this).parent().parent().children('.status').text() == "已完成"){
                status_flag = false;
                layer.msg("请别选择状态为已完成的行！",{icon : 5});
                return false;
            } else {
                detail_ids.push($(this).val());
            }
        });
        if (status_flag == false) {
            return false;
        }
        if (detail_ids.length < 1) {
            layer.msg("请选择至少一行！",{icon : 5});
            return false;
        }

        $("input[name=cancel_ids]").val(detail_ids.join(','));
    });

    /**
     * 显示已备货需求已完成的平台计划单的状态
     */
    function showProgress(status ,rowObj) {
        var progress = '';
        if (status == 100) {
            progress = '<tr class="step">'+
                    '<td colspan="17">'+
                    '<ul class="nav nav-pills nav-justified step step-progress">' +
                    '<li value="100" class="active"><a>取消</a></li>' +
                    '</ul>' +
                    '</td>'+
                    '</tr>';
        } else {
            progress = '<tr class="step">'+
                    '<td colspan="17">' +
                    '<ul class="nav nav-pills nav-justified step step-progress">' +
                    '<li value="10"><a>待推送仓库</a></li>' +
                    '<li value="20"><a>待录入箱唛数据</a></li>' +
                    '<li value="30"><a>待装箱确认</a></li>' +
                    '<li value="40"><a>待物流计划</a></li>' +
                    '<li value="50"><a>待发货</a></li>' +
                    '<li value="60"><a>已发货</a></li>' +
                    '<li value="70"><a>已完成</a></li>' +
                    '</ul>' +
                    '</td>'+
                    '<tr>';
        }
        rowObj.after(progress);
        //rowObj.next('.step').html(progress);
        rowObj.next('.step').children().children().children().each(function () {
            if ($(this).val() == status) {
                $(this).attr('class', 'active');
                $(this).prevAll().each(function () {
                    $(this).attr('class', 'active');
                })
            }

        })
    }
    function compliance() {
        if ($("#user").val() != 1 && $("#user").val() != 34) {
            layer.msg("您非合规组人员！", {icon: 5});
            return false;
        }

        var status_flag = true;
        var complianceArray = [];
        $("input[name='subBox']:checked").each(function () {
            if($(this).parent().nextAll('.status').text() != "待合规预审"){
                status_flag = false;
                layer.msg("请选择状态为待合规预审的需求",{icon : 5});
                return false;
            }else{
                var complianceData = {};
                var com_opt_str = '';
                complianceData.id = $(this).val();
                complianceData.site_id = $(this).parent().nextAll('.site_id').text();
                complianceData.sku = $(this).parent().nextAll('.sku').children().text();
                complianceData.compliance = '';
                $(this).parent().nextAll('.compliance').children('label').each(function () {
                    if ($(this).children().is(':checked')) com_opt_str += $(this).children().val() + ",";

                });

                if (com_opt_str) complianceData.compliance = com_opt_str.substring(0, com_opt_str.length - 1);
                complianceData.comment = $(this).parent().nextAll('.compliance').children('.com_comment').val();
                complianceArray.push(complianceData);
            }
        });

        if(status_flag == false){
            return false;
        }

        if (complianceArray.length < 1) {
            layer.msg("您没有选中任何内容！", {icon : 5});
            return false;
        }

        $.ajax({
            type : 'POST',
            url  : "__URL__/compliancePreview",
            data : {data:complianceArray},
            dataType : "json",
            success : function (val) {
                if (val) {
                    layer.msg("状态已更新！",{icon : 1});
                } else {
                    layer.msg("更新状态失败，请重试！",{icon : 5});
                }
                window.location.reload();
            },
            error : function () {
                layer.msg("请求出错！",{icon : 5});
                window.location.reload();
            }
        })
    }

    /**
     * 物流预审功能
     */
    function preview() {
        if ($("#user").val() != 1 && $("#user").val() != 7) {
            layer.msg("您非物流人员！", {icon: 5});
            return false;
        }
        var status_flag = true;
        var stockingArray = [];
        $("input[name='subBox']:checked").each(function () {
            if($(this).parent().nextAll('.status').text() != "待物流预审"){
                status_flag = false;
                layer.msg("请选择状态为待物流预审的需求",{icon : 5});
                return false;
            }else{
                var stockingData = {};
                stockingData.id = $(this).val();
                stockingData.single_ship = $(this).parent().nextAll('.singleShip').children().val();
                stockingArray.push(stockingData);
            }
        });

        if(status_flag == false){
            return false;
        }

        if (stockingArray.length < 1) {
            layer.msg("您没有选中任何内容！", {icon : 5});
            return false;
        }

        $.ajax({
            type : 'POST',
            url  : "__URL__/logisticsPreview",
            data : {data:stockingArray},
            dataType : "json",
            success : function (val) {
                if (val) {
                    layer.msg("状态已更新！",{icon : 1});
                } else {
                    layer.msg("更新状态失败，请重试！",{icon : 5});
                }
                window.location.reload();
            },
            error : function () {
                layer.msg("请求出错！",{icon : 5});
                window.location.reload();
            }
        })
    }

    /**
     * 取消操作
     * khq 2017.1.13
     */
    function cancel(){
        var flag   = true;
        var status = $("input[name=cancel_status]").val();
        if(status=='已完成'){
            flag = false;
            layer.msg('该状态无法取消', {icon: 5});
        }
        var id     = $("input[name=cancel_id]").val();
        if(flag && id){
            $.get("Inbound/Prepareneeds/cancel",
                    {id: id},
                    function (data) {
                        if(data.status==0) layer.msg(data.info);
                        var obj = jQuery.parseJSON(data);
                        if (obj.status == 'Y') {
                            layer.msg('操作成功', {icon: 6});
                            window.location.reload();
                        } else {
                            layer.msg(obj.message, {icon: 5});
                        }
                    });
        }
    }

    function batch_cancel() {
        var ids = $("input[name=cancel_ids]").val();
        if (ids) {
            $.get(
                "Inbound/Prepareneeds/batch_cancel",
                {ids : ids},
                function (data) {
                    var obj = jQuery.parseJSON(data);
                    if (obj.status == 'Y') {
                        layer.msg('操作成功', {icon: 6});
                        window.location.reload();
                    } else {
                        layer.msg(obj.message, {icon: 5});
                    }
                }
            )
        }
    }

    /**
     * 填充编辑数据
     * khq 2017.1.12
     */
    $(".edit_detail").click(function(){
        var id = $(this).attr('data-id');
        if (id) {
            $.get("Inbound/Prepareneeds/ajax_select_detail",
                    {id: id},
                    function (data) {
                        var obj = jQuery.parseJSON(data);
                        if (obj.status == 'Y') {
//                            console.log(obj.message.cargo_grade);
                            //$(".batch_code_edit").html(obj.message.batch_code);
                            $(".status_edit").html(obj.message.status_name);
                            $(".sku_edit").html(obj.message.sku);
                            $(".account_name_edit").html(obj.message.account_name);
                            $(".site_id_edit").html(obj.message.site_id);
                            $(".site").html(obj.message.site);
                            $("input[name=needs_quantity_edit]").val(obj.message.needs_quantity);
                            $("input[name=compare_quantity]").val(obj.message.needs_quantity);
                            $("input[name=sku_cnname]").val(obj.message.sku_name);
                            $("input[name=remark]").val(obj.message.remark);
                            $("select[name=cargo_grade]").val(obj.message.cargo_grade);
                            $("textarea[name=overseas_supervision]").val(obj.message.overseas_supervision);
                            $("input[name=id_edit]").val(id);
                            $("select[name=export_tax_rebate_edit]").val(obj.message.export_tax_rebate);
                            if(obj.message.status==10){
                                $("input[name=needs_quantity_edit]").removeAttr("readonly");
                                $("input[name=sku_cnname]").removeAttr("readonly");
                                $("input[name=remark]").removeAttr("readonly");
                                $("select[name=export_tax_rebate_edit]").removeAttr("readonly");
                            }else if(obj.message.status==20){
                                $("input[name=needs_quantity_edit]").attr("readonly","readonly");
                                $("input[name=remark]").removeAttr("readonly");
                                $("input[name=sku_cnname]").attr("readonly","readonly");
                                $("select[name=export_tax_rebate_edit]").attr("readonly","readonly");
                            }else if(obj.message.status==30){
                                $("input[name=needs_quantity_edit]").removeAttr("readonly");
                                $("input[name=sku_cnname]").attr("readonly","readonly");
                                $("select[name=export_tax_rebate_edit]").attr("readonly","readonly");
                            }else{
                                $("input[name=needs_quantity_edit]").attr("readonly","readonly");
                                $("input[name=sku_cnname]").attr("readonly","readonly");
                                $("select[name=export_tax_rebate_edit]").attr("readonly","readonly");
                                $("input[name=remark]").attr("readonly","readonly");
                            }
                        } else {
                            layer.msg(obj.message, {icon: 5});
                        }
                    });
        }
    });
    /**
     * 编辑明细
     * khq 2017.1.13
     */
    function edit() {
        var id                 = $("input[name=id_edit]").val();
        var needs_quantity     = $("input[name=needs_quantity_edit]").val();
        var compare_quantity   = $("input[name=compare_quantity]").val();
        var sku_name     = $("input[name=sku_cnname]").val();
        var remark     = $("input[name=remark]").val();
        var cargo_grade     = $("select[name=cargo_grade]").val();
        var overseas_supervision     = $("textarea[name=overseas_supervision]").val();
        //var storage            = $("input[name=storage_edit]").val();
        //var actual_quanlity    = $("input[name=actual_quanlity_edit]").val();
        var export_tax_rebate  = $("select[name=export_tax_rebate_edit]").val();
        //var carrier_service_id = $("select[name=carrier_service_id_edit]").val();
        var sku=$(".sku_edit").text();
        var site=$(".site").text();

        if (Number(needs_quantity) <= 0 || Number(needs_quantity) > Number(compare_quantity)) {
            layer.msg("修改数量不能为0或者不能大于原来的数量", {icon : 5});
            return false;
        }

        $.get("Inbound/Prepareneeds/ajax_edit",
                {
                    id: id,
                    needs_quantity:needs_quantity,
                    sku_name:sku_name,
                    remark:remark,
                    cargo_grade:cargo_grade,
                    overseas_supervision:overseas_supervision,
                    //storage:storage,
                    //actual_quanlity:actual_quanlity,
                    export_tax_rebate:export_tax_rebate,
                    sku:sku,
                    site:site
                    //carrier_service_id:carrier_service_id
                },
                function (data) {
                    if(data.status==0) layer.msg(data.info);
                    var obj = jQuery.parseJSON(data);
                    if (obj.status == 'Y') {
                        layer.msg(obj.message, {icon: 6});
                        window.location.reload();
                    } else {
                        layer.msg(obj.message, {icon: 5});
                    }
                });

    }

    /**
     * 创建FBA平台计划单
     * khq 2017.1.10
     */
    function create_fba() {
        var singleShipArray     = [];
        var sellerNameArray     = [];
        var skuArray            = [];
        var accountNameArray    = [];
        var TaxRebateArray      = [];
        var singleShipFlag      = false;
        var bproFlag            = false;

        $('.create_fba').attr('disabled', 'disabled');

        $('input[name="subBox"]:checked').each(function () {
            var sellerName = $(this).parent().nextAll('.seller').text();
            sellerNameArray.push(sellerName);

            var accountName = $(this).parent().nextAll('.account_name').text();
            if (accountName.match(/^Bpro-*/i) != null && accountName != "Bpro-US") {
                bproFlag = true;
                return false;
            }
            accountNameArray.push(accountName);

            var sku = $(this).parent().nextAll('.sellerSku').text() + ':' + $(this).parent().nextAll('.sku').text();
            skuArray.push(sku);

            var singleShipVal = $(this).parent().nextAll('.singleShip').children().val();
            if (singleShipVal == 1) {
                singleShipFlag = true;
            }
            singleShipArray.push(singleShipVal);

            var taxRebate = $(this).parent().nextAll('.export_tax_rebate').text();
            TaxRebateArray.push(taxRebate);

        });

        if (bproFlag == true) {
            layer.msg("Bpro欧洲站不能创建平台计划单", {icon : 5});
            return false;
        }

        var diffSkuFlag = checkRepeat(skuArray);
        if(skuArray.length > 1 && diffSkuFlag == true) {
            layer.msg("SKU不能重复！", {icon : 5});
            return false;
        }

        var diffTaxRebateFlag = multiValCheck(TaxRebateArray);
        if (TaxRebateArray.length > 1 && diffTaxRebateFlag == true) {
            layer.msg("非退和退税不能同时生产平台计划！", {icon : 5});
            return false;
        }

        var diffAccountNameFlag = multiValCheck(accountNameArray);
        if (accountNameArray.length > 1 && diffAccountNameFlag == true) {
            layer.msg("不能有多个帐号！", {icon : 5});
            return false;
        }

        var diffSellerFlag = multiValCheck(sellerNameArray);
        if (sellerNameArray.length > 1 && diffSellerFlag == true) {
            layer.msg("不能有多个不同销售员！", {icon : 5});
            return false;
        }

        if (singleShipArray.length > 1 && singleShipFlag == true) {
            var str = JSON.stringify(singleShipArray);
            if (str.indexOf('0') > -1) {
                layer.msg("多个SKU备货计划不能包含单独发货的SKU！", {icon : 5});
                return false;
            }

        }

        var ids = chk();//选中的id
        var remark = $('input[name="plan_remark"]').val();
        if (!remark) {
            layer.msg("请输入运输方式！", {icon : 5});
            return false;
        }

        if (ids) {
            $.get("Inbound/Prepareneeds/create_fba_stocking_plan",
                    {
                        ids : ids,
                        remark : remark
                    },
                    function (data) {
                        if(data.status==0) layer.msg(data.info);
                        var obj = jQuery.parseJSON(data);
                        if (obj.status == 'Y') {
                            layer.msg(obj.message, {icon: 6});
                            window.location.reload();
                        } else {
                            layer.msg(obj.message, {icon: 5});
                        }
                    });
        }
    }

    /**
     * 检查SKU是否重复
     */
    function checkRepeat(array){
        var arrStr = JSON.stringify(array);
        for (var i = 0; i < array.length; i++) {
            if (arrStr.indexOf(array[i]) != arrStr.lastIndexOf(array[i])){
                return true;
            }
        }

        return false;
    }
    
    function multiValCheck(array) {
        var temp;
        for (var i = 0; i < array.length; i++) {
            temp = array[0];
            if (temp != array[i]) {
                return true;
            }
        }
        return false;
    }

    /**
     * 各角色核对 1销售2仓库3物流
     */
    function check(num) {
        var ids = chk();//选中的id
        var status = '';
        var transport_way = '';
        var flag = true;
        var url  = '';
        switch (num) {
            case 1:          //销售核对
                status = 10; //当前状态10
                url    = 'check_sale';
                break;
            case 2:          //仓库核对
                status = 20; //当前状态20
                url    = 'check_warehouse';
                break;
            case 3:          //物流核对
                status = 30; //当前状态30
                url    = 'check_transport';
                transport_way = $("select[name='transport_way_hedui']").val();
                if(transport_way==''){
                    layer.msg('未选择承运商服务', {icon: 5});
                    flag = false;
                }
        }

        if (flag && ids) {
            $.get("Inbound/Prepareneeds/"+url,
                    {ids: ids, status: status,carrier_service_id:transport_way},
                    function (data) {
                        if(data.status==0) layer.msg(data.info);
                        var obj = jQuery.parseJSON(data);
                        if (obj.status == 'Y') {
                            layer.msg(obj.message, {icon: 6});
                            window.location.reload();
                        } else {
                            layer.msg(obj.message, {icon: 5});
                        }
                    });
        }
    }

    /**
     * 获取选中的id
     */
    function chk() {
        var obj = document.getElementsByName('subBox'); //选择所有的对象，返回数组
        // 取到对象数组后，我们来循环检测它是不是被选中
        var s = '';
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked) s += obj[i].value + ',';   //如果选中，将value添加到变量s中
        }
        if (s == '') {
            layer.msg('你还没有选择任何内容', {icon: 5});    //alert(s==''?'你还没有选择任何内容！':s);
            return false;
        }
        return s;
    }
    /**
     * @returns {string}
     * 获取搜索条件
     * khq 2017.1.14
     */
    function search_params(){
        var param = '';
        var sku = $("input[name='sku']").val();
        var seller_sku = $("select[name='seller_sku']").val();
        var account_id = $("select[name='account_id']").val();
        var site_id = $("select[name='site_id']").val();
        var status = $("select[name='status']").val();
        var export_tax_rebate = $("select[name='export_tax_rebate']").val();
        var create_time_from = $("input[name='create_time_from']").val();
        var create_time_to = $("input[name='create_time_to']").val();
        var claim_arrive_time_from = $("input[name='claim_arrive_time_from']").val();
        var claim_arrive_time_to = $("input[name='claim_arrive_time_to']").val();
        var compliance_check_time_from = $("input[name='compliance_check_time_from']").val();
        var compliance_check_time_to = $("input[name='compliance_check_time_to']").val();
        var create_user_id = $("input[name='create_user_id']").val();

        if($.trim(create_user_id) != ''){
            param += "&create_user_id="+$.trim(create_user_id);
        }
        if($.trim(sku) != ''){
            param += "&sku="+$.trim(sku);
        }
        if($.trim(seller_sku) != ''){
            param += "&sku="+$.trim(seller_sku);
        }
        if($.trim(account_id) != ''){
            param += "&account_id="+$.trim(account_id);
        }
        if($.trim(site_id) != ''){
            param += "&site_id="+$.trim(site_id);
        }
        if($.trim(status) != ''){
            param += "&status="+$.trim(status);
        }
        if($.trim(export_tax_rebate) != ''){
            param += "&export_tax_rebate="+$.trim(export_tax_rebate);
        }
        if($.trim(create_time_from) != '' && $.trim(create_time_to) != ''){
            param += "&create_time_from="+$.trim(create_time_from);
            param += "&create_time_to="+$.trim(create_time_to);
        }
        if($.trim(claim_arrive_time_from) != '' && $.trim(claim_arrive_time_to) != ''){
            param += "&claim_arrive_time_from="+$.trim(claim_arrive_time_from);
            param += "&claim_arrive_time_to="+$.trim(claim_arrive_time_to);
        }
        if($.trim(compliance_check_time_from) != ''){
            param += "&compliance_check_time_from="+$.trim(compliance_check_time_from);
        }
        if($.trim(compliance_check_time_to) != ''){
            param += "&compliance_check_time_to="+$.trim(compliance_check_time_to);
        }
        return param;
    }
    function download_csv() {
        var param = search_params();
        //alert('__URL__/all_detail?downcsv=1' + param);return ;
        window.location.href = '__URL__/all_detail?downcsv=1' + param;
    }
    function download_csv_other() {
        var param = search_params();
        //alert('__URL__/all_detail?downcsv=1' + param);return ;
        window.location.href = '__URL__/all_detail?downcsvother=1' + param;
    }
    function download_compliance() {
        var param = search_params();
        window.location.href = '__URL__/download_compliance?download=1' + param;
    }

</script>